import axios from "axios";
import store from "./store";

export default {
    instance: null,
    getBaseUrl () {
        return "http://"+store.state.apiHost;
    },
    createNew () {
        const baseUrl = this.getBaseUrl();
        const result = axios.create({
            baseURL: baseUrl,
            timeout: 30000
        });
        // 添加请求拦截器，在请求头中加token
        result.interceptors.request.use(
            config => {

                if (localStorage.getItem('longitude')) {
                    config.headers.gis4_longitude = localStorage.getItem('longitude');
                }
                if (localStorage.getItem('latitude')) {
                    config.headers.gis4_latitude = localStorage.getItem('latitude');
                }
                if (localStorage.getItem('gis4_imei')) {
                    config.headers.gis4_imei = localStorage.getItem('gis4_imei')
                }
                if (localStorage.getItem('gis4_phone')) {
                    config.headers.gis4_phone = localStorage.getItem('gis4_phone');
                }
                /*if (localStorage.getItem('gis4_time')) {
                    config.headers.gis4_time = localStorage.getItem('gis4_time');
                }*/
                config.headers.gis4_time = new Date().getTime();
                if (localStorage.getItem('version')) {
                    config.headers.version = localStorage.getItem('version');
                }
                if (localStorage.getItem('userToken')) {
                    config.headers.Authorization = localStorage.getItem('userToken');
                }
                if (localStorage.getItem('workModeId')!==null){
                    config.headers.gis4_workmode = localStorage.getItem('workModeId');
                }
                if (localStorage.getItem('planNo')!==null){
                    config.headers.WorkPlanNo = localStorage.getItem('planNo');
                } else{
                    config.headers.WorkPlanNo = '';
                }
                return config;
            },

            error => {
                console.log(error);
                return Promise.reject(error);
            }
        );
        result.interceptors.response.use(
            response => {
                let res={};
                res.status=response.status;
                res.data=response.data;

                /*const code = response.data ? response.data.code : '';*/

                if (res.status === 401) {
                    router.replace({ name: 'login' });
                    store.commit('token', '');
                    return Promise.reject(response.data);//跳转登陆页面
                }
                if (res.status >= 300) {
                    return Promise.reject(response.data);//返回错误状态数据
                }
                return res;
                /*return response.data.data;*///返回正确状态数据
            },
            error => {
                console.log(error);
                /*if(error.response && error.response.status == 404){
                    router.push('/blank.vue')//可做404处理
                }*/
                /*return Promise.reject(error)*/
                return Promise.reject(error.response)

            }
        );
        this.instance = result;
        return this.instance;
    },
    update (newHost) {
        this.instance = null;
        this.instance = this.createNew()
    },
    getInstance () {
        if(this.instance === null) {
            this.instance = this.createNew()
        }
        return this.instance
    }
}
